using System;
using System.Web.Mvc;
using Uber.WebUI.Models;

namespace Uber.WebUI.Controllers
{
    using Core.Service;

    [Authorize]
    public class ChangePasswordController : Controller
    {
        private readonly IAccounts accounts;

        public ChangePasswordController(IAccounts accounts)
        {
            this.accounts = accounts;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(ChangePasswordInput input)
        {
            if (!ModelState.IsValid) return View();

            if (input.ConfirmNewPassword != input.NewPassword) 
                ModelState.AddModelError("_FORM", "New Password doesn't match Confirm password");

            if (!accounts.IsPassRight(Convert.ToInt64(User.Identity.Name), input.CurrentPassword)) 
                ModelState.AddModelError("CurrentPassword", "Current password not correct");

            if (!ModelState.IsValid) return View();
            
            accounts.ChangePass(long.Parse(User.Identity.Name), input.NewPassword);

            return RedirectToAction("Success");
        }

        public ActionResult Success()
        {
            return View();
        }

    }
}
